package com.ruoyi.nuclein.service.impl;

import java.util.List;

import com.ruoyi.basicdata.domain.Resident;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.nuclein.Feign.BasicDataFeign;
import com.ruoyi.nuclein.domain.Mission;
import com.ruoyi.nuclein.mapper.MissionMapper;
import com.sun.xml.internal.bind.v2.TODO;
import org.apache.poi.xwpf.usermodel.TOC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.nuclein.mapper.ResidentmissionMapper;
import com.ruoyi.nuclein.domain.Residentmission;
import com.ruoyi.nuclein.service.IResidentmissionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户核酸表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class ResidentmissionServiceImpl implements IResidentmissionService 
{
    @Autowired
    private ResidentmissionMapper residentmissionMapper;
    @Autowired
    private MissionMapper missionMapper;
    @Autowired
    private BasicDataFeign basicDataFeign;

    /**
     * 查询用户核酸表
     * 
     * @param id 用户核酸表主键
     * @return 用户核酸表
     */
    @Override
    public Residentmission selectResidentmissionById(Long id)
    {
        return residentmissionMapper.selectResidentmissionById(id);
    }

    /**
     * 查询用户核酸表列表
     * 
     * @param residentmission 用户核酸表
     * @return 用户核酸表
     */
    @Override
    public List<Residentmission> selectResidentmissionList(Residentmission residentmission)
    {
        return residentmissionMapper.selectResidentmissionList(residentmission);
    }

    /**
     * 新增用户核酸表
     * 
     * @param residentmission 用户核酸表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertResidentmission(Residentmission residentmission){
        // 根据任务id 查询任务开始时间，结束时间，检测位置
        Mission mission = missionMapper.selectMissionById(residentmission.getNucleinMissionId());
        residentmission.setStartTime(mission.getStartTime());
        residentmission.setEndTime(mission.getEndTime());
        residentmission.setCheckAddress(mission.getCheckAddress());
        residentmission.setState(1L); // 待检查

        // 将任务人数+1
        missionMapper.updateMissionByBuildingId(mission.getBuildingId());
        return residentmissionMapper.insertResidentmission(residentmission);
    }

    /**
     * 修改用户核酸表
     * 
     * @param residentmission 用户核酸表
     * @return 结果
     */
    @Override
    public int updateResidentmission(Residentmission residentmission){
        return residentmissionMapper.updateResidentmission(residentmission);
    }

    /**
     * 批量删除用户核酸表
     * 
     * @param ids 需要删除的用户核酸表主键
     * @return 结果
     */
    @Override
    public int deleteResidentmissionByIds(Long[] ids){
        return residentmissionMapper.deleteResidentmissionByIds(ids);
    }

    /**
     * 删除用户核酸表信息
     * 
     * @param id 用户核酸表主键
     * @return 结果
     */
    @Override
    public int deleteResidentmissionById(Long id)
    {
        return residentmissionMapper.deleteResidentmissionById(id);
    }


    /**
     *  根据住户Id获取任务名字
     * @param residentId
     * @return
     */
    @Override
    public AjaxResult getMissNameByResidentId(Long residentId) {
        return residentmissionMapper.getMissNameByResidentId(residentId);
    }

    @Override
    public List<Residentmission> selectResidentByIds(List<Long> ids) {
        return residentmissionMapper.selectResidentByIds(ids);
    }


    /**
     * 用户完成核酸
     * @param residentId
     * @return
     */
    @Override
    @Transactional
    public AjaxResult successMission(Long residentId) {
        // 修改用户核酸的状态， 以及检测时间
        residentmissionMapper.updateByResidentId(residentId);
        basicDataFeign.updateStateById(residentId);
        // TODO 修改任务完成人数+1
        AjaxResult missNameByResidentId = residentmissionMapper.getMissNameByResidentId(residentId);
        Long id = (Long) missNameByResidentId.get("nuclein_mission_id");
        missionMapper.updateCompleteNumById(id);
        return AjaxResult.success();
    }



}